/*
 * ExcelRuleBaseLoader.java
 *
 * Created on 2006-4-30
 * Original Author: Schweigen
 * 
 * Changes
 * ---------
 * $Log: ExcelRuleBaseLoader.java,v $
 * Revision 1.1  2009/06/22 11:51:39  cvs
 * *** empty log message ***
 *
 * Revision 1.1  2009/05/05 08:59:24  cvs
 * *** empty log message ***
 *
 *
 */

package com.et114.components.rule.support;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.compiler.PackageBuilder;
import org.drools.decisiontable.InputType;
import org.drools.decisiontable.SpreadsheetCompiler;
import org.springframework.core.io.Resource;

import java.io.StringReader;

/**
 */
public class ExcelRuleBaseLoader extends AbstractFileRuleBaseLoader {

    public RuleBase buildRuleBase() throws Exception {

        RuleBase ruleBase = RuleBaseFactory.newRuleBase();
        SpreadsheetCompiler converter = new SpreadsheetCompiler();
        Resource[] resources = getRuleFiles(ruleFiles);
        for ( int i = 0 ; i < resources.length ; i ++  ) {
        	Resource resource = resources [ i ] ;
            PackageBuilder builder = new PackageBuilder();
            String drl = converter.compile(resource.getInputStream(), InputType.XLS);
            builder.addPackageFromDrl(new StringReader(drl));
            org.drools.rule.Package pkg = builder.getPackage();
            ruleBase.addPackage(pkg);
        }

        return ruleBase;
    }
}
